package com.itheima.produce;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer_PubSub {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("192.168.17.139");//默认为127.0.0.1
        factory.setPort(5672);
        factory.setVirtualHost("/itcast");//虚拟机
        factory.setUsername("zwy");//用户名
        factory.setPassword("123456");//密码
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        //创建交换机
        /*
        交换机名称  交换机类型（枚举）
       String exchange,
       BuiltinExchangeType type,  DIRECT定向 FANOUT广播 TOPIC通配符 HEADERS参数匹配
        boolean durable, 是否持久化
        boolean autoDelete, 自动删除
        boolean internal,   是否内部使用
         Map<String, Object> arguments 参数
         */
        String exchangeName="test_fanout";
        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.FANOUT,true,false,false,null);
        //创建队列
        String queue1="test_fanout1";
        String queue2="test_fanout2";
        channel.queueDeclare(queue1,true,false,false,null);
        channel.queueDeclare(queue2,true,false,false,null);

        //绑定队列与交换机
        /*
        队列名    交换机名    路由健（绑定规则）交换机类型为 FANOUT，key为空
        String queue, String exchange, String routingKey, Map<String, Object> arguments
         */
        channel.queueBind(queue1,exchangeName,"");
        channel.queueBind(queue2,exchangeName,"");
        //发送消息
        String body="日志信息，张三调用了findAll方法....:info...";
        channel.basicPublish(exchangeName,"",null,body.getBytes());
        //释放资源
        channel.close();
        connection.close();
    }
}
